<?php

namespace App\Models;

use App\Constant\Enum;
use App\Constant\User\UserMode;
use App\Models\Contract\SerializeDatetime;
use App\Services\User\BlockUserService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Laravel\Sanctum\HasApiTokens;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasApiTokens, HasFactory, Notifiable, SerializeDatetime;


    protected $fillable = [
        'parent_id',
        'uuid',
        'username',
        'password',
        'dip',
        'phone',
        'nickname',
        'avatar',
        'status',
        'mode',
        'remark',
        'openid',
        'cid',
        'pid',
        'is_new',
        'is_admin_related',
        'is_show_ad',
        'is_group_leader',
        'is_advanced_group',
        'registered_at',
        'last_login_at',
        'last_login_ip',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'registered_at' => 'datetime',
        'last_login_at' => 'datetime',
    ];

    public function getMode(): array
    {
        $mode = $this->attributes['mode'];
        return [
            'disable_withdraw' => ($mode & UserMode::DISABLE_WITHDRAW) === UserMode::DISABLE_WITHDRAW,
            'disable_share' => ($mode & UserMode::DISABLE_SHARE) === UserMode::DISABLE_SHARE,
        ];
    }

    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    public function member()
    {
        return $this->hasOne(GroupMember::class);
    }

    public function wallet()
    {
        return $this->hasOne(UserWallet::class);
    }

    public function adrewards()
    {
        return $this->hasMany(Adreward::class);
    }

    public function userwallet()
    {
        return $this->hasMany(UserWallet::class, 'user_id');
    }


    public function userwithdraws()
    {
        return $this->hasMany(UserWithdraw::class, 'user_id');
    }


    public function logined()
    {
        $ip = request()->getClientIp() ?? '-';
        $this->update([
            'last_login_at' => Carbon::now(),
            'last_login_ip' => $ip,
        ]);
    }

    public function markAs(string $status, $mode = 0, $remark = '', $isShowAd = 1)
    {
        if ($status == Enum::STATUS_ENABLED) {
            $remark = '';
        }

        if ($status != Enum::STATUS_ENABLED) {
            BlockUserService::ban($this, [
                'status' => $status,
                'mode' => $mode,
                'remark' => $remark,
                'is_show_ad' => $isShowAd,
            ]);
        } else {
            $this->update([
                'status' => $status,
                'mode' => $mode,
                'remark' => $remark,
                'is_show_ad' => $isShowAd,
            ]);
        }
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }
}
